<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/28
 * Time: 10:27
 */

namespace app\adminapiv3\service\platform_manage;


use app\adminapiv3\service\BaseService;
use think\Db;

class ServerAllowIpService extends BaseService
{
    /**
     * @name 列表
     * @param $params
     * @return array
     * @author wx
     * @date 2019/10/28 10:36
     */
    public function list($params)
    {
        $server = new \app\common\service\platform_manage\ServerAllowIpService();
        $data = $server->list($params);

        //处理时间
        foreach($data['data'] as $k => $v){
            if($v['login_time']){
                $v['login_time'] = date('Y-m-d H:i:s',$v['login_time']);
            }else{
                $v['login_time'] = '暂未登录';
            }

            $data['data'][$k] = $v;
        }

        return $data;
    }

    /**
     * @name 添加
     * @param $addData
     * @return bool
     * @author wx
     * @date 2019/10/28 10:50
     */
    public function add($addData)
    {
        global $logArray;
        $logArray = array(
            'content' => '增加IP',
        );

        //查看重复
        if ($this->checkExist(['ip' => $addData['ip']])) {
            $this->setError($this->CODE_FAIL, '请勿添加重复ip');
            return false;
        }

        $addData['login_time'] = 0;
        $addData['ctime'] = time();

        $server = new \app\common\service\platform_manage\ServerAllowIpService();
        $server->add($addData);

        $logArray['new_info'] = "IP地址：{$addData['ip']}";

        return true;
    }

    /**
     * @name 编辑
     * @param $id
     * @param $updateData
     * @return bool
     * @author wx
     * @date 2019/10/28 10:50
     */
    public function modify($id, $updateData)
    {
        //记录操作日志
        global $logArray;
        $logArray = array(
            'content' => '修改IP',
        );
        if (isset($updateData['id'])) {
            unset($updateData['id']);
        }
        $info = Db::name('server_allow_ip')->where('id',$id)->find();
        if(!$info){
            $this->setError($this->CODE_FAIL,'数据不存在');
        }

        //查看重复
        if ($this->checkExist([
            ['ip' ,'eq' ,$updateData['ip']],
            ['id','neq',$id]
        ])) {
            $this->setError($this->CODE_FAIL, '请勿添加重复ip');
            return false;
        }

        $server = new \app\common\service\platform_manage\ServerAllowIpService();
        $server->modify(['id' => $id], $updateData);

        $logArray['old_info'] = "IP地址：{$info['ip']}";
        $logArray['new_info'] = "IP地址：{$updateData['ip']}";

        return true;
    }

    /**
     * @name 删除
     * @param $id
     * @return bool
     * @author wx
     * @date 2019/10/28 10:50
     */
    public function del($id)
    {
        global $logArray;
        $logArray = array(
            'content' => '删除IP',
        );

        $condition[] = array('id', 'in', $id);

        //查询数据
        $list = Db::name('server_allow_ip')->where('id','in',$id)->column('ip');

        $server = new \app\common\service\platform_manage\ServerAllowIpService();
        $server->del($condition);

        //记录操作日志
        $logArray['old_info'] = '一共删除'.count($list).'个IP；IP为'.implode('，',$list);

        return true;
    }

    public function checkExist($condition)
    {
        $server = new \app\common\service\platform_manage\ServerAllowIpService();
        $info = $server->findData($condition);

        if(!$info){
            return false;
        }

        return true;
    }

}